//
//  MQMainViewController.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/10.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MQMainViewController: UITabBarController {

    override func viewDidLoad() {
        let tabBar = MQTabBar()
        
        setValue(tabBar, forKey: "tabBar")
        
        tabBar.composeButtonClickClouser = {
//            printfLog("点击composeButton")
//            let keyWindow = UIApplication.sharedApplication().keyWindow
//            let window = UIApplication.sharedApplication().windows
//            window?.addSubview(MQComposeView())
//            window?.rootViewController?.view.addSubview(MQComposeView())
            let btnView = MQComposeView()
            btnView.showView(self)
            
        }
        addChildViewControllers()
    }
    
    //添加四个子控制器
    func addChildViewControllers() {
        addChildViewController(MQHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(MQMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(MQDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(MQProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }
    
    //添加子控制器
    func addChildViewController(childController: UIViewController, imageName:String, title:String){
        
        
        childController.tabBarItem = MQTabBarItem()
        //当图片与titile在一起时设置图片的偏移
//        childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        
        //此处必须这样使用，才可以在MQNavigationController中设置backTitle = childViewControllers.first!.title ?? "返回"
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        childController.tabBarItem.title = title
        
        //指定字体颜色的方式1
         UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        //指定字体颜色的方式2
//        let dict = [
//            NSForegroundColorAttributeName:UIColor.orangeColor()
//        ]
//        
//        childController.tabBarItem.setTitleTextAttributes(dict, forState: UIControlState.Selected)
        addChildViewController(MQNavigationController(rootViewController: childController))
    }
}
